package com.api.task.edittask;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.test.context.ContextConfiguration;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import com.api.test.AbstractApiTest;
import com.test.util.ApiUtil;
import com.zyncro.api.ITaskApiService;
import com.zyncro.api.exceptions.ZyncroApiException;
import com.zyncro.api.model.TaskApi;
import com.zyncro.api.model.TaskApiType;
import com.zyncro.api.model.TaskStatusApiType;

@ContextConfiguration(locations = { "classpath:/task/task-context.xml" })
public class EditTaskTaskManager extends AbstractApiTest {

	@Value("#{group_toEdit[taskManager].appId}")
	private String taskManagerUrn;

	@Value("#{group_toEdit[taskManager].password}")
	private String taskManagerPassword;

	@Value("#{group_toEdit.taskUrn}")
	private String groupTaskUrn;

	private String titleBefore;

	private String descriptionBefore;

	private TaskApiType type;

	@BeforeClass
	protected void setParameters() throws Exception {

		getApi().login(taskManagerUrn, taskManagerPassword);

		TaskApi task = getApi().getService(ITaskApiService.class).getTask(groupTaskUrn).getObject();

		this.titleBefore = task.getTitle();
		this.descriptionBefore = task.getDescription();
		this.type = ApiUtil.asEnum(TaskApiType.class, task.getType());
	}

	@Test
	public void testEditTask() throws ZyncroApiException {

		String title = "jaja";
		String description = "jeje";
		String responsible = taskManagerUrn;
		String responsibleText = null;
		String dueDate = null;
		TaskApiType type = TaskApiType.DECISION;
		TaskStatusApiType status = null;
		String following = null;

		getApi().getService(ITaskApiService.class).editTask(groupTaskUrn, title, description, responsible, responsibleText, dueDate, type,
				status, following);

		TaskApi task = getApi().getService(ITaskApiService.class).getTask(groupTaskUrn).getObject();

		Assert.assertEquals(task.getTitle(), title, "Task title");
		Assert.assertEquals(task.getDescription(), description, "Task description");
		Assert.assertEquals(task.getType(), type.getId(), "Task type");
	}

	@AfterClass
	public void restoreValues() throws ZyncroApiException {

		getApi().getService(ITaskApiService.class).editTask(groupTaskUrn, titleBefore, descriptionBefore, null, null, null, type, null,
				null);

	}
}
